package com.jinshiji.newclient;

import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.jinshiji.newclient.com.jinshiji.newclient.service.NewsInfoService;
import com.jinshiji.newclient.java.util.ArrayList.bean.NewsItem;

import java.util.List;

public class MainActivity extends AppCompatActivity {

    private ListView listView;
    private List<NewsItem> allNews;
    private static final int SUCCESS = 1;
    private static final int ERROR = 2;
    private String TAG = "MainActivity";

    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case SUCCESS:
                    NewsAdapter newsAdapter = new NewsAdapter();
                    listView.setAdapter(newsAdapter);
//                    newsAdapter.notifyDataSetChanged();
                    break;
                case ERROR:
                    Toast.makeText(MainActivity.this, "加载数据失败!", Toast.LENGTH_SHORT).show();
                    break;
            }
        }
    };


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        initData();
    }

    private void initView() {
        listView = (ListView) findViewById(R.id.lv_listView);
    }

    private void initData() {
        //获取服务器的数据
        new Thread() {
            @Override
            public void run() {
                try {
                    allNews = NewsInfoService.getAllNews(getApplicationContext().getResources()
                            .getString(R.string.serverpath));
                    //更新界面的操作放在ui线程中执行
                    Message msg = new Message();
                    msg.what = SUCCESS;
                    handler.sendMessage(msg);
                } catch (Exception e) {
                    e.printStackTrace();
                    //加载数据失败
                    Message msg = new Message();
                    msg.what = ERROR;
                    handler.sendMessage(msg);
                }
            }
        }.start();
    }

    private class NewsAdapter extends BaseAdapter {
        private Context context;

        @Override
        public int getCount() {
            return allNews.size();
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
//            View view = null;
            ViewHolder holder;
            //创建Item显示布局
            if (convertView == null) {  //判断view 是否为空 view是形式参数
                convertView = View.inflate(MainActivity.this, R.layout.item_news2, null);
                holder = new ViewHolder(); //
                //在view查找子控件 并给view设置 相关信息
                holder.iv_picture = (ImageView) convertView.findViewById(R.id.iv_item_picture);
                holder.tv_title = (TextView) convertView.findViewById(R.id.tv_item_title);
                holder.tv_describe = (TextView) convertView.findViewById(R.id.tv_item_desc);
                holder.tv_comment = (TextView) convertView.findViewById(R.id.tv_comment);
                convertView.setTag(holder);
            } else {
                holder = (ViewHolder)convertView.getTag();//??
            }
            //获取Item并给各个控件设置 内容
            NewsItem newsItem = allNews.get(position);
            holder.iv_picture.setImageResource(R.drawable.p2);
            holder.tv_title.setText(newsItem.getTitle());
            holder.tv_describe.setText(newsItem.getDescription()                                                                                                                                                    );
            Log.i(TAG,"描述的信息为:"+newsItem.getDescription());
           //翻译结果及顾客
            holder.tv_title.setText(newsItem.getTitle());
            Log.i(TAG,"返回信息的标题为"+newsItem.getTitle());
            String type = newsItem.getType();
            if("1".equals(type)){
                holder.tv_comment.setText("评论:"+newsItem.getComment());
            }else if("2".equals(type)){
                holder.tv_comment.setText("视频");
                holder.tv_comment.setBackgroundColor(0x66ff0000);
            }else if("3".equals(type)){
                holder.tv_comment.setText("LIVE");
                holder.tv_comment.setBackgroundColor(0x660000ff);
            }
            return convertView;
        }

        @Override
        public Object getItem(int position) {
            Log.i(TAG, "getItem的position" + position);
            return null;
        }

        @Override
        public long getItemId(int position) {
            Log.i(TAG, "getItemId的position" + position);
            return 0;
        }

    }

    public static class ViewHolder {
        public ImageView iv_picture;
        public TextView tv_title;
        public TextView tv_describe;
        public TextView tv_comment;

    }
}
